﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Inspinia_MVC5.Models;
using Inspinia_MVC5.Data;
using Inspinia_MVC5.Help;


namespace Inspinia_MVC5.Controllers
{
    [AdminAuthorize]
    public class DashBoardController : Controller
    {
        private BooksSystemContext db = new BooksSystemContext();

        // GET: DashBoard
        public ActionResult Index()
        {
            List<DepartmentInfo> departmentInfos = new List<DepartmentInfo>();
            string statusName = "";
            string stutus1 = "";
            var group = db.Borrows.Where(x=>x.Role==0).GroupBy(x => x.Student.Class.Department.DepartName);
            var statusGroup = db.Borrows.GroupBy(x=>x.Status);

            foreach (var item in group)
            {
                DepartmentInfo departmentInfo = new DepartmentInfo();
                departmentInfo.Name = item.Key;
                departmentInfo.JieYue = item.Count(x=>x.Status==1);
                departmentInfo.XuJie = item.Count(x=>x.Status==2);
                departmentInfos.Add(departmentInfo);

            }
            foreach (var item in statusGroup)
            {
                if (item.Key == 1)
                {
                    statusName += "已借阅"+",";
                    stutus1 += item.Count().ToString()+",";
                }

                else if (item.Key == 2)
                {
                    statusName += "已续借"+",";
                    stutus1 += item.Count().ToString()+",";

                }
                else if (item.Key == 3)
                {
                    statusName += "已还书"+",";
                    stutus1 += item.Count().ToString()+",";

                }

            }
            int stuNum = db.Borrows.Count(x=>x.Role==0);
            int TeaNum = db.Borrows.Count(x=>x.Role==1);
            string departmentInfosXi = "";
            string departmentInfosJie = "";
            string departmentInfosXu = "";
            foreach (var item in departmentInfos)
            {
                departmentInfosXi += item.Name+",";
                departmentInfosJie += item.JieYue.ToString() + ",";
                departmentInfosXu += item.XuJie.ToString() + ",";

            }
            DateTime dstart = DateTime.Now.Date;
            DateTime dend = dstart.AddDays(1);
            int jinriJie = db.Borrows.Count(x => x.JieYueDate > dstart && x.JieYueDate < dend);
            int jinriXu = db.Borrows.Count(x => x.XuJieDate > dstart && x.XuJieDate < dend);
            int jinriHuan = db.Borrows.Count(x => x.ReturnDate > dstart && x.ReturnDate < dend);
            ViewBag.jinriJie = jinriJie;
            ViewBag.jinriXu = jinriXu;
            ViewBag.jinriHuan = jinriHuan;
            ViewBag.statusName = statusName;
            ViewBag.status1 = stutus1;
            ViewBag.departmentInfosXi = departmentInfosXi;
            ViewBag.departmentInfosJie = departmentInfosJie;
            ViewBag.departmentInfosXu = departmentInfosXu;
            ViewBag.stuNum = stuNum;
            ViewBag.TeaNum = TeaNum;
            return View();
        }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}